package list;

import java.util.Arrays;

public class MyArrayList implements IList{
    public int[] elem;
    public int usedSize;
    private static final int DEFAULT_SIZE = 10;

    public MyArrayList(){
        this.elem = new int[DEFAULT_SIZE];
    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
    }

    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    private void grow(){
        this.elem = Arrays.copyOf(this.elem,2 * elem.length);
    }

    @Override
    public boolean isFull() {
        return this.usedSize == elem.length;
    }

    private void checkPosOfAdd(int pos) throws PosIllegal{
        if(pos < 0 || pos > usedSize){
            throw new PosIllegal("Pos位置不合法！");
        }
    }
    @Override
    public void add(int pos, int data) {
        try{
            checkPosOfAdd(pos);
            if(isFull()){
                grow();
            }

            for(int i = this.usedSize - 1;i >= pos;i--){
                this.elem[i + 1] = this.elem[i];
            }
            this.elem[pos] = data;
            this.usedSize++;
        }catch(PosIllegal e){
            System.out.println("插入元素pos位置不合法...");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) {
        boolean flg = false;
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                flg = true;
                break;
            }
        }
        return flg;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    private void checkPosOfGet(int pos) throws PosIllegal{
        if(pos < 0 || pos >= usedSize){
            throw new PosIllegal("Pos位置不合法！");
        }
    }
    private void checkEmpty() throws EmptyException{
        if (isEmpty()){
            throw new EmptyException("顺序表为空！");
        }
    }
    public boolean isEmpty(){
        return this.usedSize == 0;
    }
    @Override
    public int get(int pos) {
        try{
            checkEmpty();
            checkPosOfGet(pos);
            return this.elem[pos];
        }catch(EmptyException e){
            e.printStackTrace();
        }catch(PosIllegal e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkEmpty();
            checkPosOfGet(pos);
            this.elem[pos] = value;
        }catch(PosIllegal e){
            e.printStackTrace();
        }catch(EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int key) {
        try{
            checkEmpty();
            int pos = indexOf(key);
            if(pos == -1){
                return;
            }
            for (int i = pos; i < this.usedSize - 1; i++) {
                this.elem[i] = this.elem[i + 1];
            }
            this.usedSize--;
        }catch(EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        this.usedSize = 0;
    }
}
